#

| Chapter 31 Creating Custom Commands in vi |

## 31.5 Keymaps for Pasting into a Window Running vi

I usually run vi inside windows on a system like X or the Macintosh. The window systems can copy and paste text between windows. Pasting into a vi window may be tricky if you use vi options like wrapmargin or autoindent-the text you paste can be rearranged or indented in weird ways.

I've fixed that with the keymaps below. If I'm pasting in text that should be copied exactly with no changes, I go into text-input mode and type CTRL-x. That shuts off autoindent (`noai`) and the wrapmargin (`wm=0`). When I'm done pasting, I type CTRL-n while I'm still in text-input mode.

A different kind of "pasted" input is with CTRL-r. It starts the fmt (35.2) utility to reformat and clean up lines while I'm pasting them. To use it, go to text-input mode and type CTRL-r. Then paste the text-fmt will read it but not display it. Press RETURN, then CTRL-d to end the standard input to fmt. The reformatted text will be read into your vi buffer.

    ^[
    " Set 'exact' input mode for pasting exactly what is entered:
    map! ^X ^[:se noai wm=0^Ma
    " Set 'normal' input mode with usual autoindent and wrapmargin:

    map! ^N ^[:se ai wm=8^Ma
    " Read pasted text, clean up lines with fmt. Type CTRL-d when done:
    map! ^R ^[:r!fmt^M

Note that some window systems convert TAB characters to spaces when you copy and paste. If you want the TABs back, try a filter-through (30.22) with unexpand (24.6).

- JP

31.4 vi @-Functions |  | 31.6 Protecting Keys from Interpretation by ex